package com.localjoycommunity.cpdj.project.system.controller;

import com.localjoycommunity.cpdj.common.interceptor.help.NeedLogin;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;
import com.localjoycommunity.cpdj.project.system.domain.SysCourse;
import com.localjoycommunity.cpdj.project.system.domain.UserCourse;
import com.localjoycommunity.cpdj.project.system.domain.UserCourseWatch;
import com.localjoycommunity.cpdj.project.system.service.ISysCourseService;
import com.localjoycommunity.cpdj.project.system.service.IUserCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程管理Controller
 *
 * @author leepandar
 * @date 2020-11-12
 */
@RestController
@RequestMapping("/system/course")
public class SysCourseController extends BaseController {
    @Autowired
    private ISysCourseService sysCourseService;
    @Autowired
    private IUserCourseService userCourseService;

    /**
     * 用户收藏课程
     */
    @NeedLogin
    @PostMapping("/digCourse")
    public AjaxResult digCourse(@RequestBody UserCourse userCourse, HttpServletRequest req) {
        Integer userId = getUserId(req);
        userCourse.setUserId(userId);
        int i = userCourseService.courseDig(userCourse);
        return AjaxResult.success(i);
    }

    /**
     * 查询课程管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysCourse sysCourse) {
        startPage();
        List<SysCourse> list = sysCourseService.selectSysCourseList1(sysCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysCourse sysCourse) {
        List<SysCourse> list = sysCourseService.selectSysCourseList(sysCourse);
        ExcelUtil<SysCourse> util = new ExcelUtil<SysCourse>(SysCourse.class);
        return util.exportExcel(list, "course");
    }

    /**
     * 获取课程管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysCourseService.selectSysCourseById(id));
    }

    /**
     * 获取课程管理详细信息app
     */
    @GetMapping(value = "/app/{id}")
    public AjaxResult getInfoApp(@PathVariable("id") Long id, HttpServletRequest req) {
        SysCourse sysCourse = sysCourseService.selectSysCourseByIdApp(id);
        Integer userId = getUserId(req);
        if (userId != 0){
            Integer dig = userCourseService.isDig(userId, id);
            sysCourse.setIsDig(dig);
            userCourseService.updateUserWatchNum(userId,id);
        }
        return AjaxResult.success(sysCourse);
    }

    /**
     * 观看之后返回的时候调用
     * 记录当前的观看进度
     */
    @PostMapping(value = "/app/watch")
    public AjaxResult watch(@RequestBody UserCourseWatch userCourseWatch,HttpServletRequest req) {
        Integer userId = getUserId(req);
        userCourseWatch.setUserId(userId);
        userCourseService.updateUserWatchProgess(userCourseWatch);
        return AjaxResult.success();
    }

    /**
     * 新增课程管理
     */
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCourse sysCourse) {
        return toAjax(sysCourseService.insertSysCourse(sysCourse));
    }

    /**
     * 修改课程管理
     */
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCourse sysCourse) {
        return toAjax(sysCourseService.updateSysCourse(sysCourse));
    }

    /**
     * 删除课程管理
     */
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysCourseService.deleteSysCourseByIds(ids));
    }
}
